package pacs_server

import (
	"encoding/json"
	"fmt"
	"gitee.com/uni-minds/bridge-pacs/global"
	"gitee.com/uni-minds/bridge-pacs/tools"
	"github.com/gohouse/gorose/v2"
)

func (pi *PacsServer) DbStudiesHandler() (h gorose.IOrm) {
	table := fmt.Sprintf("%s_studies", pi.TableTitle)
	h = DB().Table(table)

	if !pi.ShowHidden {
		h.Where("db_hidden", 0)
	}

	if !pi.ShowDelete {
		h.Where("db_delete", 0)
	}

	return h
}
func (pi *PacsServer) DbSeriesHandler() (h gorose.IOrm) {
	table := fmt.Sprintf("%s_series", pi.TableTitle)
	h = DB().Table(table)

	if !pi.ShowHidden {
		h.Where("db_hidden", 0)
	}

	if !pi.ShowDelete {
		h.Where("db_delete", 0)
	}

	return h
}
func (pi *PacsServer) DbInstanceHandler() (h gorose.IOrm) {
	table := fmt.Sprintf("%s_instance", pi.TableTitle)
	h = DB().Table(table)

	if !pi.ShowHidden {
		h.Where("db_hidden", 0)
	}

	if !pi.ShowDelete {
		h.Where("db_delete", 0)
	}

	return h
}

func (pi *PacsServer) decodeStudiesInfos(data []gorose.Data) (infos []global.StudiesInfo, err error) {
	bs, _ := json.Marshal(data)
	err = json.Unmarshal(bs, &infos)
	return infos, err
}
func (pi *PacsServer) decodeStudiesInfo(data gorose.Data) (info global.StudiesInfo, err error) {
	bs, _ := json.Marshal(data)
	err = json.Unmarshal(bs, &info)
	return info, err
}
func (pi *PacsServer) decodeSeriesInfos(data []gorose.Data) (infos []global.SeriesInfo, err error) {
	bs, _ := json.Marshal(data)
	err = json.Unmarshal(bs, &infos)
	return infos, err
}
func (pi *PacsServer) decodeSeriesInfo(data gorose.Data) (info global.SeriesInfo, err error) {
	bs, _ := json.Marshal(data)
	err = json.Unmarshal(bs, &info)
	return info, err
}
func (pi *PacsServer) decodeInstanceInfos(data []gorose.Data) (infos []global.InstanceInfo, err error) {
	bs, _ := json.Marshal(data)
	err = json.Unmarshal(bs, &infos)
	return infos, err
}
func (pi *PacsServer) decodeInstanceInfo(data gorose.Data) (info global.InstanceInfo, err error) {
	bs, _ := json.Marshal(data)
	err = json.Unmarshal(bs, &info)

	if info.Duration == -1 {
		pi.checkInstanceMedia(info)
		return info, err
	} else {
		return info, nil
	}
}

// region DB Operation
func (pi *PacsServer) dbCreateStudies(info global.StudiesInfo) (err error) {
	table := fmt.Sprintf("%s_studies", pi.TableTitle)
	_, err = DB().Table(table).Insert(info)
	//pi.Logf("%d studies records created.\n", i)
	return err
}
func (pi *PacsServer) dbRemoveStudies(studiesId string) (err error) {
	pi.Logf("removing studies id: %s\n", studiesId)
	_, err = pi.DbStudiesHandler().Where("studies_id", studiesId).Delete()
	return err
}
func (pi *PacsServer) dbUpdateStudies(id string, data map[string]interface{}) (err error) {
	data["update_datetime"] = tools.TimeEncodeNow()
	_, err = pi.DbStudiesHandler().Data(data).Where("studies_id", id).Update()
	//pi.Logf("%d studies records updated.\n", i)
	return err
}

func (pi *PacsServer) dbCreateSeries(info global.SeriesInfo) (err error) {
	table := fmt.Sprintf("%s_series", pi.TableTitle)
	_, err = DB().Table(table).Insert(info)
	//pi.Logf("%d series records created\n", i)
	return err
}
func (pi *PacsServer) dbRemoveSeries(seriesId string) (err error) {
	pi.Logf("removing series id: %s\n", seriesId)
	_, err = pi.DbSeriesHandler().Where("series_id", seriesId).Delete()
	return err
}
func (pi *PacsServer) dbUpdateSeries(seriesId string, data map[string]interface{}) (err error) {
	data["update_datetime"] = tools.TimeEncodeNow()
	_, err = pi.DbSeriesHandler().Data(data).Where("series_id", seriesId).Update()
	//pi.Logf("%d series records updated.\n", i)
	return err
}

func (pi *PacsServer) dbCreateInstance(info global.InstanceInfo) (err error) {
	table := fmt.Sprintf("%s_instance", pi.TableTitle)
	_, err = DB().Table(table).Insert(info)
	//pi.Logf("%d instance records created.\n", i)
	return err
}
func (pi *PacsServer) dbRemoveInstance(instanceId string) (err error) {
	pi.Logf("removing instance id: %s\n", instanceId)
	_, err = pi.DbInstanceHandler().Where("instance_id", instanceId).Delete()
	return err
}
func (pi *PacsServer) dbUpdateInstance(instanceId string, data map[string]interface{}) (err error) {
	_, err = pi.DbInstanceHandler().Data(data).Where("instance_id", instanceId).Update()
	//pi.Logf("%d instance records updated.\n", i)
	return err
}

//endregion
